﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ListeningSystem
{
    public static class SqlHelper
    {
        private static readonly string constr = ConfigurationManager.ConnectionStrings["sqlconnect"].ConnectionString;

        /// <summary>
        ///  //1.执行增删改  add  delete update
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pms"></param>
        /// <returns></returns>
        public static int ExecuteNonQuery(string sql, params SqlParameter[] pms)
        {
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand(sql, con))
                {
                    if (pms != null)
                    {
                        cmd.Parameters.AddRange(pms);
                    }
                    con.Open();
                    return cmd.ExecuteNonQuery();

                }

            }

        }


        /// <summary>
        ///  //2.执行查询返回单个值的方法  select 
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pms"></param>
        /// <returns></returns>
        public static object ExecuteScalar(string sql, params SqlParameter[] pms)
        {
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand(sql, con))
                {
                    if (pms != null)
                    {
                        cmd.Parameters.AddRange(pms);
                    }
                    con.Open();
                    return cmd.ExecuteScalar();

                }

            }

        }

        //3.执行查询 返回多行多列的方法

        public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] pms)
        {

            SqlConnection con = new SqlConnection(constr);

            using (SqlCommand cmd = new SqlCommand(sql, con))
            {
                if (pms != null)
                {
                    cmd.Parameters.AddRange(pms);
                }
                try
                {
                    con.Open();
                    //System.Data.CommandBehavior.CloseConnection,在关闭reader的同时，在sqlreader内部会将关联的Connection对象也关闭掉
                    return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);

                }
                catch
                {
                    con.Close();
                    con.Dispose();
                    throw;
                }
            }
        }
        //4.查询数据返回DataTable
        public static DataTable ExecuteDataTable(string sql, params SqlParameter[] pms)
        {
            DataTable datatable = new DataTable();
            using (SqlDataAdapter adpter = new SqlDataAdapter(sql, constr))
            {
                if (pms != null)
                {
                    adpter.SelectCommand.Parameters.AddRange(pms);
                }
                adpter.Fill(datatable);
            }
            return datatable;
        }

    }

}
